package com.moon.game.command.guest.lair;

import java.util.ArrayList;
import java.util.List;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.service.ChatService;
import com.moon.game.service.LairService;
import com.moon.game.service.PlayerService;
import com.moon.game.util.GDateUtils;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.Tower;

/**
 * @description 修理孵化塔
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-14 下午11:22:14
 */
public class RepairLairCmd extends BaseCmd {

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		PlayerVo player = req.getPlayer();
		List<Tower> towers = player.getTowerList();
		if (towers == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "进入孵化塔出错，你当前没有孵化塔！"));
			return;
		}
		Integer towerId = Integer.parseInt(ps[0]);
		if (towerId == null) {
			doResponsePsError(req);
			return;
		}
		Tower theTower = LairService.findLair(towers, towerId);
		//没有指定孵化塔
		if (theTower == null) {
			//清除修理消息
			ChatService.removeThePrivateActMessage(req.getPlayerId(), Constants.REPAIR_LAIR, String.valueOf(towerId));
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "你当前没有指定的孵化塔！"));
			return;
		}
		//检测自然侵蚀
		int erode = LairService.calcMinusLairEndure(2, GDateUtils.now() - theTower.getLastRepariTime());
		if (erode > theTower.getCurrEndure()) {
			List<Tower> newTowerList = new ArrayList<Tower>();
			for (Tower tower : towers) {
				if (tower.getTowerId() == towerId) {
					continue;
				}
				newTowerList.add(tower);
			}
			player.setTowerList(newTowerList);
			PlayerService.doUpdatePlayer(player.getSid(), player.getId(), player);
			String info = "你的孵化塔因自然侵蚀到持久底线已经导致倒塌，塔内珍兽在孵化塔倒塌时已经离开！";
			//清除修理消息
			ChatService.removeThePrivateActMessage(req.getPlayerId(), Constants.REPAIR_LAIR, String.valueOf(towerId));
			req.setVO(TAG_INFO, info);
			//返回
			response(req, req.getClientResponse());
			return;
		}
		
		//计算修复需要扣除的费用
		int price = LairService.getRepaireMoney(erode, theTower.getHierarchy());
		
		Integer sure = Integer.parseInt(ps[1]);
		if (sure == null) {
			doResponsePsError(req);
			return;
		}
		
		if (sure.intValue() == 1) {
			//确认修理
			boolean minusStatu = PlayerService.minusMoney(req.getSid(), req.getPlayerId(), price);
			if (!minusStatu) {
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_GOLD, "你的碎银不足" + price + "，不能修复孵化塔！"));
				return;
			}
			theTower.setCurrEndure(theTower.getEndure());
			theTower.setLastRepariTime(GDateUtils.now());
			//清除修理消息
			ChatService.removeThePrivateActMessage(req.getPlayerId(), Constants.REPAIR_LAIR, String.valueOf(theTower.getTowerId()));
		}else {
			//待确认
			req.setVO(TAG_ACK, true);
		}
		req.setVO("tower_id", towerId);
		req.setVO("price", price);
		PlayerService.doUpdatePlayer(req.getSid(), req.getPlayerId(), player);
		response(req, req.getClientResponse());
	}
}
